<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<th:block th:include="system_public::header"></th:block>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="padding:5px 0;">
		<div id="search" class="search">
			<table>
				<tr>
					<td class="label">用户名</td>
 					<td>
 						<input type="text" name="loginname">
 					</td>
 					<td class="label">姓名</td>
 					<td>
 						<input type="text" name="fullname">
 					</td>
 					<td class="label">用户类型</td>
 					<td>
 						<select name="loginUserTypeId">
 							<option value="0">所有类型</option>
 							<option th:each="n : ${loginUserTypeList}" th:value="${n.id}" th:text="${n.typename}"></option>
 						</select>
 					</td>
 				</tr>
 				<tr>
 					<td colspan="8" style="height:30px;" align="center">
 						<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" id="searchButton">查询</a>
 						<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" id="addButton">增加</a>
 						<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-group_key'" id="batchPwdButton">重置密码</a>
 					</td>
				</tr>
			</table>
		</div>
	</div>
	<div data-options="region:'center',border:false" style="padding:0 5px;">
		<table id="list"></table>
	</div>
	<div id="addOrUpdateDialog" style="display:none;">
		<form method="post">
			<input type="hidden" name="id">
			<table class="addform">
				<tr>
					<td class="title" colspan="2"></td>
				</tr>
				<tr>
					<td class="label">用户名</td>
					<td class="input">
						<input type="text" name="loginname">
					</td>
				</tr>
				<tr>
					<td class="label">姓名</td>
					<td class="input">
						<input type="text" name="fullname">
					</td>
				</tr>
				<tr>
					<td class="label">用户类别</td>
					<td class="input">
						<select name="loginUserTypeId">
							<option value="0">请选择用户类型</option>
							<option th:each="n : ${loginUserTypeList}" th:value="${n.id}" th:text="${n.typename}"></option>
						</select>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<th:block th:include="system_public::footer"></th:block>
	<th:block th:include="system_public::jsfile"></th:block>
	<script th:inline="none">
	$(function(){
		var $search = $('#search');
		var $list = $('#list');
		var $addOrUpdateDialog = $('#addOrUpdateDialog');
		var $addOrUpdateDialog_form = $addOrUpdateDialog.children('form');
		$list.datagrid({
			title : '用户列表',
			fit : true,
			url : pageurl+'/system/loginuser/index',
			pagination : true,
			rownumbers : true,
			pageList:[10,30,50,100],
			pageSize : 50,
			onBeforeLoad : function(param) {
				param.loginname = $search.find('[name=loginname]').val();
				param.fullname = $search.find('[name=fullname]').val();
				param.loginUserTypeId = $search.find('[name=loginUserTypeId]').val();
			},
			columns : [[{
				field : 'id',
				checkbox : true
			},{
				title : '用户名',
				field : 'loginname',
				width : 100
			},{
				title : '姓名',
				field : 'fullname',
				width : 100
			},{
				title : '用户类型',
				field : 'loginUserType',
				width : 100,
				formatter : function(value) {
					return value.typename;
				}
			},{
				title : '操作',
				field : 'c',
				width : 100,
				formatter : function(value, rowData, rowIndex) {
					var $span = $("<span></span>").addClass("upload");
					var $updateBtn = $("<a></a>").attr("href","javascript:;").attr("onclick", "updateRow("+rowIndex+")").text("修改").appendTo($span);
					var $deleteBtn = $("<a></a>").attr("href","javascript:;").attr("onclick", "deleteRow("+rowIndex+")").text("删除").appendTo($span);
					$deleteBtn.css("padding-left","5px");
					return $span.prop("outerHTML");
				}
			}]]
		});
		$addOrUpdateDialog.show().dialog({
			title : '操作用户',
			iconCls : 'icon-edit',
			modal : true,
			closed : true,
			width : 400,
			height : 250,
			buttons : [{
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					$addOrUpdateDialog_form.form('submit');
				}
			},{
				text : '返回',
				iconCls : 'icon-back',
				handler : function() {
					$addOrUpdateDialog.dialog("close");
				}
			}],
			onClose : function() {
				$addOrUpdateDialog_form.form("reset");
				$addOrUpdateDialog_form.find('input[name=loginname]').prop('readonly',false);
			}
		});
		$addOrUpdateDialog_form.form({
			success : function(data) {
				var obj = $.parseJSON(data);
				if(obj.success) {
					$.messager.alert('系统提示','操作成功','info');
					$addOrUpdateDialog.dialog("close");
					$list.datagrid("reload");
				} else {
					$.messager.alert('info',obj.message,'warning');
				}
			},
			onLoadError : function(error) {
				$.messager.alert('info',error,'warning');
			}
		});
		$('#searchButton').click(function(){
			$list.datagrid('load');
		});
		$('#addButton').click(function(){
			$addOrUpdateDialog.dialog('open');
			$addOrUpdateDialog_form.attr('action',pageurl+'/system/loginuser/add');
			$addOrUpdateDialog_form.find('td.title').text('增加新用户(密码与用户名一致)');
		});
		window.updateRow = function(rowIndex) {
			var rows = $list.datagrid("getRows");
			var row = rows[rowIndex];
			$addOrUpdateDialog.dialog('open');
			$addOrUpdateDialog_form.attr('action',pageurl+'/system/loginuser/update');
			$addOrUpdateDialog_form.find('td.title').text('修改用户');
			$addOrUpdateDialog_form.form('load',row);
			$addOrUpdateDialog_form.find('input[name=loginname]').prop('readonly',true);
		}
		window.deleteRow = function(rowIndex) {
			var rows = $list.datagrid("getRows");
			var row = rows[rowIndex];
			$.messager.confirm('系统提示','您确定要删除'+row.loginname+'吗？',function(r){
				if(r) {
					$.post(pageurl+'/system/loginuser/remove',{
						id : row.id
					},function(obj){
						if(obj.success) {
							$.messager.alert('系统提示','操作成功','info');
							$list.datagrid("reload");
						} else {
							$.messager.alert('info',obj.message,'warning');
						}
					},'json');
				}
			});
		}
		$('#batchPwdButton').click(function(){
			var rows = $list.datagrid('getSelections');
			if(rows.length>0) {
				$.messager.confirm('系统提示','您确定要重置这'+rows.length+'位用户密码吗？',function(r){
					if(r) {
						var array = [];
						$.each(rows,function(i,n){
							array.push(n.id);
						});
						$.post(pageurl+'/system/loginuser/reset_pwd',{
							ids : array.join(',')
						},function(obj){
							if(obj.success) {
								$.messager.alert('系统提示','重置成功，新密码与用户名相同','info');
							} else {
								$.messager.alert('info',obj.message,'warning');
							}
						},'json');
					}
				});
			} else {
				$.messager.alert('系统提示','请至少选择一个用户','info');
			}
		});
	});
	</script>
</body>
</html>